<template>
  <div class="home">
    <el-container>
      <el-header>
        <div>
          <img src="../assets/heima.png" alt="">
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="exist">退出</el-button>
      </el-header>
      <el-container>

        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class="collapseBtn" @click="ifShowMenu">|||</div>

          <!-- 导航栏 -->
          <el-menu background-color="#313743" text-color="#fff" active-text-color="#409EFF" :unique-opened='true'
            :collapse="isCollapse" :collapse-transition="false" :default-active="activePath" router>

            <!-- 一级菜单内容 -->
            <el-submenu :index="'/' + item.path" v-for="item in menuList" :key="item.id">
              <template slot="title">
                <i :class="iconList[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>

              <!-- 二级菜单模板 -->
              <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id"
                @click="setNavSeeionFun('/' + subItem.path)">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>

        <!-- 主体 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>

export default {
  data () {
    return {
      menuList: '',
      iconList: {
        125: 'el-icon-user-solid',
        103: 'el-icon-coin',
        101: 'el-icon-goods',
        102: 'el-icon-shopping-bag-1',
        145: 'el-icon-data-line'
      },
      isCollapse: false
    }
  },
  methods: {
    data () {
      return {
        activePath: ''
      }
    },
    // 退出登录
    exist () {
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
    },

    // 请求左侧菜单
    async getMenusFun () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message({ message: res.meta.msg, type: 'error' })
      this.menuList = res.data
    },
    // 菜单是否折叠
    ifShowMenu () {
      this.isCollapse = !this.isCollapse
    },
    // 设置激活菜单
    setNavSeeionFun (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = window.sessionStorage.getItem('activePath')
    }

  },
  created () {
    this.getMenusFun()
    // 获取激活菜单
    this.activePath = window.sessionStorage.getItem('activePath')
  }
}
</script>

<style lang="scss" scoped>
.home {
  height: 100%;
  width: 100%;
}

.el-container {
  height: 100%;
  width: 100%;
}

.el-header {
  height: 100%;
  width: 100%;
  background-color: #343b3e;
  display: flex;
  padding-left: 0;
  justify-content: space-between;

  >div {
    display: flex;
    align-items: center;
    font-size: 20px;
    color: #fff;

    img {
      width: 100px;
    }

    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #313743;

  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #e9edf0;
}

.collapseBtn {
  font-size: 12px;
  line-height: 24px;
  text-align: center;
  color: #fff;
  background-color: #465261;
  letter-spacing: 0.2em;
}
</style>
